<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends BaseController
{

    public function edit(Request $request){
        $id = $request->get('id');
        $data = User::find($id);
        return view('admin.user.edit',compact('data'));
    }

    public function update(Request $request)
    {
        $id = $request->get('id');
        $params = $this->validate($request, [
            'username' => 'required',
            'truename' => 'required',
            'password' => 'required',
            'email' => 'required',
            'phone' => 'required',
            'sex' => 'required'
        ]);
        $params['password'] = bcrypt($params['password']);
        $res = User::where('id',$id)->update($params);

        if ($res) {
            return redirect(route("admin.user.index"));
        } else {
            return redirect(route("admin.user.edit"))->withErrors("添加失败");
        }

    }

    public function index()
    {
        $data = User::withTrashed()
            ->paginate($this->pageSize);
        return view('admin.user.index', compact('data'));
    }

    public function add()
    {
        return view('admin.user.add');
    }


    public function create(Request $request)
    {
        $params = $this->validate($request, [
            'username' => 'required',
            'truename' => 'required',
            'password' => 'required',
            'email' => 'required',
            'phone' => 'required',
            'sex' => 'required'
        ]);
            $params['password'] = bcrypt($params['password']);
            $res = User::create($params);

            if ($res) {
                return redirect(route("admin.user.index"));
            } else {
                return redirect(route("admin.user.add"))->withErrors("添加失败");
            }

    }


    public function del(Request $request)
    {
        $id = $request->get('id');
        $res = User::destroy($id);
        if ($res) {
            return redirect(route('admin.user.index'));
        } else {
            return redirect(route('admin.user.index'));
        }

    }


    public function restore(Request $request)
    {
        $id = $request->get('id');
        $res = User::withTrashed()
            ->where('id', $id)
            ->restore();
        if ($res) {
            return redirect(route('admin.user.index'));
        } else {
            return redirect(route('admin.user.index'));
        }

    }


    public function role(User $user,Request $request)
    {
        if ($request->isMethod('get')) {
            $role = Role::all();
            $currentRoleID = $user->role()->value('id');
            return view('admin.user.role',compact('role','user','currentRoleID'));
        }else{
            $user->role_id = $request->get('role_id');
            $res = $user->save();

            if ($res) {
                return redirect(route("admin.user.index"));
            } else {
                return redirect(route("admin.user.index"))->withErrors("分配角色失败");
            }
        }
    }
}
